﻿using IdentityModel;
using IdentityServer4.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using static IdentityModel.OidcConstants;

namespace AuthServer
{
    public class ApiConfig
    {
        /// <summary>
        /// 定义API资源
        /// </summary>
        /// <returns></returns>
        public static IEnumerable<ApiResource> GetApiResources()
        {
            return new[]
            {
                new ApiResource("Service1","服务1"){
                Scopes={ "Service1"}//这里不加Scopes会报：Audience validation failed. Audiences: 'empty'. Did not match: validationParameters.ValidAudience: 'Service1' or validationParameters.ValidAudiences: 'null'
              
                },
               new ApiResource("Service2","服务2"){
               Scopes={"Service2"}//这里不加Scopes会报：Audience validation failed. Audiences: 'empty'. Did not match: validationParameters.ValidAudience: 'Service1' or validationParameters.ValidAudiences: 'null'
               }
            };
        }
        /// <summary>
        /// 定义作用域
        /// </summary>
        /// <returns></returns>
        public static IEnumerable<ApiScope> GetApiScopes()
        {
            return new[]
            {
                new ApiScope("Service1"),
                new ApiScope("Service2"),
            };
        }
        /// <summary>
        /// 定义客户端
        /// </summary>
        /// <returns></returns>
        public static IEnumerable<Client> GetClients()
        {
            return new[]
            {
                new Client{
                    ClientId="Client1",//客户端ID
                    ClientSecrets=new[]
                    {
                        new Secret("zy".ToSha256())//客户端密码，进行了加密
                    },
                    AllowedGrantTypes=IdentityServer4.Models.GrantTypes.ClientCredentials,//授权方式，这里采用的是客户端认证模式，只要ClientId，以及ClientSecrets正确即可访问对应的AllowedScopes里面的api资源
                    AllowedScopes=new []{"Service1","Service2"}//定义这个客户端可以使用的资源
                }
            };
        }
    }
}
